package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import modelo.Aluno;
import modelo.Turma;

import conexao.ConnectionManager;

public class TurmaDaoImp implements TurmaDAO{
	private final String INSERT = "insert into turma (curso, nome)values(?,?)";
	private final String UPDATE = "update turma set curso = ?, nome = ? where idAluno = ?";
	private final String DELETE = "delete from turma where idTurma = ?";
	private final String ALL = "select * from turma";
	private final String ID = "select * from turma where idTurma = ?";
	private final String NOME = "select * from turma where nome like ?";
	private final String CURSO = "select * from turma where curso like ?";
	
	private static Connection connection;
	
	public TurmaDaoImp() {
		openConnection();
	}
	@Override
	public Turma salvar(Turma e) {
		try{
			PreparedStatement ps = null;
			ps = ConnectionManager.getInstance().getConnection().prepareStatement(INSERT);
			ps.setObject(1, e.getCurso());
			ps.setObject(2, e.getNome());
			ps.executeUpdate();
			}catch(SQLException ex){
				Logger.getLogger(AlunoDaoImp.class.getName()).log(Level.SEVERE, null,ex);
				
			}finally{
				this.closeConnection();
			}
			return e;
	}
	@Override
	public void alterar(Turma e) {
		try{
			PreparedStatement ps = null;
			ps = ConnectionManager.getInstance().getConnection().prepareStatement(UPDATE);
			ps.setObject(1, e.getCurso());
			ps.setObject(2, e.getNome());
			ps.executeUpdate();
		}catch(SQLException ex){
			Logger.getLogger(TurmaDaoImp.class.getName()).log(Level.SEVERE,null,ex);
		}finally{
			this.closeConnection();
		}
	}
	@Override
	public void deletar(Turma e) {
		try{
			PreparedStatement ps = null;
			ps = connection.prepareStatement(DELETE);
			ps.setObject(1, e.getIdTurma());
			ps.executeUpdate();
		}catch(SQLException ex){
			Logger.getLogger(AlunoDaoImp.class.getName()).log(Level.SEVERE,null,ex);
		}finally{
			this.closeConnection();
		}
	}
	@Override
	public List findAll() {
		List<Turma> turmas = new ArrayList<Turma>();
		
		try{
			PreparedStatement ps = null;
			ResultSet rs = null;
			ps = connection.prepareStatement(ALL);
			rs = ps.executeQuery();
			while (rs.next()) {
				Turma turma = new Turma();
				turma.setIdTurma(rs.getInt("idturma"));
				turma.setCurso(rs.getString("curso"));
				turma.setNome(rs.getString("nome"));
				turmas.add(turma);
				
			}
			}catch(SQLException ex){
				Logger.getLogger(TurmaDaoImp.class.getName()).log(Level.SEVERE,null,ex);
				
			} finally {
				this.closeConnection();
			}
			return turmas;
	}

	public boolean isConnectionClose() {
		try {
			return connection.isClosed();
		} catch (SQLException ex) {
			return false;
		}
		
	}

	@Override
	public void openConnection() {
		if ( connection == null) {
			connection = ConnectionManager.getInstance().getConnection();
		}
		
		if ( isConnectionClose() ) {
			connection = ConnectionManager.getInstance().getConnection();
		}	
	}

	@Override
	public void closeConnection() {
		if ( connection != null && !this.isConnectionClose()) {
			ConnectionManager.getInstance().closeConnection(connection);
		}
	}
	@Override
	public Turma buscarPorID(int idTurma) {
		List<Turma> turmas = new ArrayList<Turma>();
		
		try{
			PreparedStatement ps = null;
			ResultSet rs = null;
			ps = connection.prepareStatement(ID);
			ps.setObject(1, idTurma);
			rs = ps.executeQuery();
			while (rs.next()) {
				Turma turma = new Turma();
				turma.setIdTurma(rs.getInt("idturma"));
				turma.setCurso(rs.getString("curso"));
				turma.setNome(rs.getString("nome"));
				turmas.add(turma);
				
			}
			}catch(SQLException ex){
				Logger.getLogger(TurmaDaoImp.class.getName()).log(Level.SEVERE,null,ex);
				
			} finally {
				this.closeConnection();
			}
			return turmas.get(0);
	}
	@Override
	public List<Turma> buscarPorCurso(String curso) {
		List<Turma> turmas = new ArrayList<Turma>();

		try {
			PreparedStatement ps = null;
			ResultSet rs = null;
			
			ps = connection.prepareStatement(CURSO);
			ps.setObject(1, curso+"%");
			
			rs = ps.executeQuery();
			
			while (rs.next()) {
				Turma turma = new Turma();
				turma.setIdTurma(rs.getInt("idturma"));
				turma.setCurso(rs.getString("curso"));
				turma.setNome(rs.getString("nome"));
				turmas.add(turma);
				
			}

		} catch (SQLException ex) {
			Logger.getLogger(TurmaDaoImp.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			this.closeConnection();
		}if (turmas.isEmpty()) {
			return null;
		} else {
			return turmas;
		}
	}
	@Override
	public List<Turma> buscarPorNome(String nome) {
		List<Turma> turmas = new ArrayList<Turma>();

		try {
			PreparedStatement ps = null;
			ResultSet rs = null;
			
			ps = connection.prepareStatement(NOME);
			ps.setObject(1, nome+"%");
			
			rs = ps.executeQuery();
			
			while (rs.next()) {
				Turma turma = new Turma();
				turma.setIdTurma(rs.getInt("idturma"));
				turma.setCurso(rs.getString("curso"));
				turma.setNome(rs.getString("nome"));
				turmas.add(turma);
				
			}

		} catch (SQLException ex) {
			Logger.getLogger(TurmaDaoImp.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			this.closeConnection();
		}if (turmas.isEmpty()) {
			return null;
		} else {
			return turmas;
		}
	}
	

}
